package com.custom.base.util

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import com.custom.base.R

/**
 * 作者：
 * 日期：2020-12-19
 * 说明：判断输入框是否为空，并改变按钮状态
 */
class EditTextNullUtil {
    private var mEditTexts: List<EditText> = listOf()
    private var mVisibilityViews: List<View> = listOf()
    private var mVisibilityType: Int = View.GONE
    private var mBtTv: TextView? = null
    private var mBtLL: LinearLayout? = null
    private lateinit var booleans: BooleanArray
    private var mNullListener: OnNullListener? = null
    private var mTrueBg = 0
    private var mFalseBg = 0

    /**
     * 添加 EditText
     * @param edit
     * @return
     */
    fun addEditText(edit: List<EditText>): EditTextNullUtil {
        mEditTexts = edit
        booleans = BooleanArray(mEditTexts.size)
        for (i in mEditTexts.indices) {
            booleans[i] = mEditTexts[i].text.toString().trim { it <= ' ' }.isNotEmpty()
            mEditTexts[i].addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
                override fun afterTextChanged(s: Editable) {
                    booleans[i] = s.toString().trim { it <= ' ' }.isNotEmpty()
                    update()
                }
            })
        }
        return this
    }

    /**
     * 添加按钮
     * @return
     */
    fun addBt(
        btTv: TextView? = null,
        btLl: LinearLayout? = null,
        trueBg:Int = R.drawable.layer_blue2_item_single_circular,
        falseBg:Int = R.drawable.layer_blue4_item_single_circular
    ): EditTextNullUtil {
        mBtLL = btLl
        mBtTv = btTv
        mTrueBg = trueBg
        mFalseBg = falseBg
        return this
    }
    /**
     * 添加隐藏
     */
    fun addVisibility(views : List<View>,visibility:Int = View.GONE): EditTextNullUtil {
        mVisibilityViews = views
        mVisibilityType = visibility
        return this
    }

    /**
     * 改变状态
     */
    private fun update() {
        if (inspectBoolean()) {
            mBtTv?.apply {
                setBackgroundResource(mTrueBg)
                isEnabled = true
            }
            mBtLL?.apply {
                setBackgroundResource(mTrueBg)
                isEnabled = true
            }
            for (view in mVisibilityViews) {
                view.visibility = View.VISIBLE
            }
        }else{
            mBtTv?.apply {
                setBackgroundResource(mFalseBg)
                isEnabled = false
            }
            mBtLL?.apply {
                setBackgroundResource(mFalseBg)
                isEnabled = false
            }
            for (view in mVisibilityViews) {
                view.visibility = mVisibilityType
            }
        }
    }

    /**
     * 判断是否全部为空
     * @return
     */
    private fun inspectBoolean(): Boolean {
        var isTrue = true
        for (aBoolean in booleans) {
            if (!aBoolean) {
                isTrue = false
                break
            }
        }
        mNullListener?.onNull(!isTrue)
        return isTrue
    }

    fun start(){
        update()
    }

    fun setOnNullListener(nullListener: OnNullListener): EditTextNullUtil {
        mNullListener = nullListener
        return this
    }

    interface OnNullListener {
        fun onNull(isNull: Boolean)
    }
}